// |------------------------------------------------------
// |------------------------------------------------------
// | Functions
// |------------------------------------------------------
// |------------------------------------------------------

//
// Str replace
//
// @param {string}  $string    String that you want to replace
// @param {string}  $substr    String that is to be replaced by `$newsubstr`
// @param {string}  $newsubstr String that replaces `$substr`
// @param {number*} $all       Flag for replaceing all (1+) or not (0)
// @return {string}
//
@function str-replace($string, $substr, $newsubstr, $all: 0) {
	$position-found: str-index($string, $substr);
	$processed: ();

	@while ($position-found and $position-found > 0) {
		$length-substr: str-length($substr);
		$processed: append($processed, str-slice($string, 0, $position-found - 1));
		$processed: append($processed, $newsubstr);
		$string: str-slice($string, $position-found + $length-substr);

		$position-found: 0;

		@if ($all > 0) {
			$position-found: str-index($string, $substr);
		}
	}

	$processed: append($processed, $string);
	$string: "";

	@each $s in $processed {
		$string: #{$string}#{$s};
	}

	@return $string;
}

//
// Map set
//
// @param 	Map 	$map 		The map to use
// @param 	String 	$key 		The key to update
// @param 	Mixed 	$value 		The new value
// @return 	Map 			The new map
//
@function map-set($map, $key, $value) {
	$new: ($key: $value);
	@return map-merge($map, $new);
}

//
// Remove item from list
//
@function remove-nth($list, $index) {
  $result: null;

  @if type-of($index) != number {
	@warn "$index: #{quote($index)} is not a number for `remove-nth`.";
  }

  @else if $index == 0 {
	@warn "List index 0 must be a non-zero integer for `remove-nth`.";
  }

  @else if abs($index) > length($list) {
	@warn "List index is #{$index} but list is only #{length($list)} item long for `remove-nth`.";
  }

  @else {
	$result: ();
	$index: if($index < 0, length($list) + $index + 1, $index);

	@for $i from 1 through length($list) {
	  @if $i != $index {
		$result: append($result, nth($list, $i));
	  }
	}
  }

  @return $result;
}

//
// List shift
//
// @param 	List 	$list 		The list to use
// @return 	List 				The processed list
//
@function list-shift($list) {
	@return remove-nth($list,1);
}

//
// List pop
//
// @param 	List 	$list 		The list to use
// @return 	List 				The processed list
//
@function list-pop($list) {
	@return remove-nth($list,length($list));
}

//
// In map
// Determine if something is in the provided map
//
// @param 	Map 	$map 		The map to use
// @param 	Mixed 	$needle		What to search
// @return 	Boolean				True|false
//
@function in-map($map, $needle) {
	@each $v in $map {
		@if $v == $needle {
			@return true;
		}
	}
	@return false;
}


//
// Get states count
//
// @return 	int 	The number of states defined
//
@function gridle_states_count() {
	@return length($_gridle_states);
}


//
// Get the current state
//
@function gridle_current_state() {
	@return $_gridle_current_state;
}


//
// Get the current state name
//
@function gridle_current_state_name() {
	@return $_gridle_current_stateName;
}


//
// Get the column width in percent for the global or a specific context
//
// @param 	int 		$columns 					The number of columns to calculate
// @param 	int 		$context : $gridle-columns-count 	 	The context to use
// @return 	percentage 							The width in percent
//
@function gridle_column_width(
	$columns : 1,
	$stateMap-or-stateName : current
) {
	$context : gridle_get_state_var(context, $stateMap-or-stateName);
	@return percentage(1 / $context * $columns);
}


//
//  Get a state map
//
// @param 	string 		$name 		The name of the state to get
// @return 	map 				A state map object
//
@function gridle_get_state(
	$stateMap-or-stateName : current
) {

	// check if need to return the current state
	@if $stateMap-or-stateName == current {
		@return gridle_current_state();
	}

	// check if has a state named like this
	@if (type-of($stateMap-or-stateName) == string
		and  map-has-key($_gridle_states, unquote("#{$stateMap-or-stateName}")))
	{
		@return map-get($_gridle_states, unquote("#{$stateMap-or-stateName}"));
	}

	// check if it's a registered state as map passed
	@if type-of($stateMap-or-stateName) == map
		and map-get($stateMap-or-stateName, name) {
		$name : map-get($stateMap-or-stateName, name);
		@if gridle_has_state($name) {
			@return $stateMap-or-stateName;
		}
	}

	// a map is passed, so it's a state himself
	@if $stateMap-or-stateName
		and type-of($stateMap-or-stateName) == map
	{
		// prepare state
		$stateMap-or-stateName : _gridle_prepare_state_settings($stateMap-or-stateName);
		// create a new state by merging given one with default one
		$state : map-merge($_gridle-settings, $stateMap-or-stateName);
		// set the name with random name
		$state : map-set($state, name, unique-id());
		// return the custom state
		@return $state;
	}

	// return the default one if exist
	@if map-has-key($_gridle_states, default)
	{
		@return map-get($_gridle_states, default);
	}

	// nothing finded, return the default state
	@return $_gridle-settings;
}


//
// Check if a state exist :
//
// @param 	string 		$name 		The name of the state to check
// @return 	Boolean 			true is exist
//
@function gridle_has_state(
	$stateName
) {
	@if $stateName == current {
		@return true;
	}
	@if map-has-key($_gridle_states, unquote("#{$stateName}")) {
		@return true;
	} @else {
		@return false;
	}
}


//
// Get a variable
//
// @param 	String 		$varName 					The variable name
// @param  	String 		$stateMap-or-stateName 	 	The state name or a map state value
// @return 	Mixed 									The finded value
//
@function gridle_get_state_var(
	$varName,
	$stateMap-or-stateName : current
) {
	// if is a state :
	$state : null;

	// get the state (if no state find, return the current one) :
	$state : gridle_get_state($stateMap-or-stateName);

	// check if has key
	@if map-has-key($state, unquote("#{$varName}")) {
		@return map-get($state, unquote("#{$varName}"));
	}

	// nothing finded :
	@return null;
}


//
// Set a variable in a state
// @param 	Mixed $stateName-or-stateIndex 	The state name of state index
// @param  	String $var                    		Variable name to assign
// @param  	Mixed $newValue          		The new value to assign
// @return 	List                         			The states list (full)
//
@function gridle_set_state_var(
	$var,
	$newValue,
	$stateName : default
) {
	// get the state :
	$state : gridle_get_state($stateName);

	// check ig state and if has the variable :
	@if $state
		  and map-has-key($state,unquote("#{$var}"))
	{
		// set new value in state :
		$state : map-set($state, unquote("#{$var}"), $newValue);

		// set states :
		$_gridle_states : map-set($_gridle_states, unquote("#{$stateName}"), $state);

		// return new state :
		@return $state;
	}

	// nothing getted :
	@return null;
}


//
// get the registered gridle states
//
@function gridle_get_states() {
	@return $_gridle_states;
}


//
// Get the states names
//
@function gridle_get_states_names() {
	$list : ();
	@each $stateName, $state in $_gridle_states {
		$list : append($list, $stateName);
	}
	@return $list;
}
@function gridle_states_names() {
	@return gridle_get_states_names();
}


//
// Get the apply css for map for a certain class and state
//
@function gridle_get_apply_css_for_map(
	$for,
	$stateName : default
) {
	// check if has some extend for this state
	$map : map-get($_gridle_apply_css_for, $stateName);
	@if $map == null { @return null; }

	// check if has some extend for the requested for
	$extend : map-get($map, $for);

	// return the resulting extend map
	@return $extend;
}


//
// Get the extend map for a certain class and state
//
@function gridle_get_extend_class_map(
	$for,
	$stateName : default
) {
	// check if has some extend for this state
	$map : map-get($_gridle_extend_base_classes, $stateName);
	@if $map == null { @return null; }

	// check if has some extend for the requested for
	$extend : map-get($map, $for);

	// return the resulting extend map
	@return $extend;
}


//
// Generate a column
//
// @param 	String 		$name 				The column name (often count)
// @param 	int 		$columns 			The column count that the column will take
// @param 	int 		$context 			The context on which the width will be calculed
// @param 	Boolean 	$generateClasses 	Set if the column has to be generated in css
//
@function _gridle_create_column(
	$name,
	$columns,
	$context,
	$name-multiplicator : 1 // used to extend the state on custom registered columns
) {
	@return (
		name : $name,
		columns : $columns,
		context : $context,
		name-multiplicator : $name-multiplicator
	);
}


// get columns names in a list
@function gridle_get_columns(
	$state : default
) {
	// get variables
	$context : gridle_get_state_var(context, $state);
	$name-multiplicator : gridle_get_state_var(name-multiplicator, $state);

	// get specials columns
	$columnsMap : map-merge((), $_gridle_columns);

	// loop through context
	@for $i from 0 through $context {

		// name
		$columnName : "#{$i*$name-multiplicator}";
		$columnWidth : $i * $name-multiplicator;

		// // create a column
		$col : _gridle_create_column($columnName, $columnWidth, $context, $name-multiplicator);

		// // add column in columns map
		$columnsMap : map-set($columnsMap, $columnName,  $col);
	}

	// return columns
	@return $columnsMap;
}


//
// Check if has column
//
@function gridle_has_column(
	$name
) {
	$column : map-get($_gridle_columns,$name);
	@if $column {
		@return true;
	} @else {
		@return false;
	}
}


@function _gridle_prepare_state_settings(
	$settings
) {
	// manage gutters
	$gutter-top : map-get($settings, gutter-top);
	$gutter-bottom : map-get($settings, gutter-bottom);
	$gutter-left : map-get($settings, gutter-left);
	$gutter-right : map-get($settings, gutter-right);
	$gutter-width : map-get($settings, gutter-width);
	$gutter-height : map-get($settings, gutter-height);
	@if $gutter-right and $gutter-left {
		// calculate the gutter-width
		$settings : map-set($settings, gutter-width, $gutter-left + $gutter-right);
	} @else if $gutter-width {
		// calculate the gutter-left and right
		$settings : map-set($settings, gutter-left, $gutter-width * .5);
		$settings : map-set($settings, gutter-right, $gutter-width * .5);
	}
	@if $gutter-top and $gutter-bottom {
		// calculate the gutter-height
		$settings : map-set($settings, gutter-height, $gutter-bottom + $gutter-top);
	} @else if $gutter-height {
		// calculate the gutter-bottom and top
		$settings : map-set($settings, gutter-bottom, $gutter-height * .5);
		$settings : map-set($settings, gutter-top, $gutter-height * .5);
	}

	// return prepared settings
	@return $settings;
}


//
// Extend a state
//
@function _gridle_inherit_state(
	$state,
	$extend-state
) {
	$extend-state-name : gridle_get_state_var(name, $extend-state);
	$state-name : gridle_get_state_var(name, $state);
	$has-state : gridle_has_state($state-name);
	$has-extend-state : gridle_has_state($extend-state-name);
	@if $has-state and $has-extend-state {
		// we do nothing if this is nested existing states
		@return $state;
	} @else if $has-extend-state {
		// if the base state is a registered one
		// keep the extend state and extend it with the wanted state
		@return map-merge($extend-state, $state);
	} @else {
		// otherwise, the base state is a custom one so we keep
		// it and extend the wanted state with it
		$new-state : map-remove($extend-state, name); // remove the name is important here!
		@return map-merge($state, $new-state);
	}
}


//
// Get the padding value
//
@function _gridle_forge_gutters_map(
	$side-or-size,
	$state : current
) {
	$map : ();
	// check if is number passed
	@if type-of($side-or-size) == map {
		@each $side in (top right bottom left) {
			@if map-get($side-or-size, $side) {
				$map : map-set($map, $side, map-get($side-or-size, $side));
			} @else {
				$map : map-set($map, $side, 0);
			}
		}
	} @else if type-of($side-or-size) == number {
		$map : (
			top : 0,
			right : $side-or-size * .5,
			bottom : 0,
			left : $side-or-size * .5
		);
	} @else if type-of($side-or-size) == list or type-of($side-or-size) == string {

		// check if is a full number list
		$list-number : true;
		@each $s in $side-or-size {
			@if type-of($s) != number {
				$list-number : false;
			}
		}

		@if $list-number {

			@if length($side-or-size) == 2 {
				$val1 : nth($side-or-size,1) * .5;
				$val2 : nth($side-or-size,2) * .5;
				$map : (
					top : $val1,
					right : $val2,
					bottom : $val1,
					left : $val2
				)
			} @else if length($side-or-size) == 4 {
				$map : (
					top : nth($side-or-size,1),
					right : nth($side-or-size,2),
					bottom : nth($side-or-size,3),
					left : nth($side-or-size,4)
				)
			}

		} @else {
			// forge the map with registered values
			@each $side in (top right bottom left) {
				@if index($side-or-size, $side) {
					$map : map-set($map, $side, gridle_get_state_var("gutter-#{$side}", $state));
				} @else {
					$map : map-set($map, $side, 0);
				}
			}
			// @debug("get gutters from registered #{inspect($map)}");
		}
	} @else {
		// unable to generate a gutter map
		@return false;
	}

	// return the padding map
	@return $map;
}


//
// Get the attribute selector
//
@function gridle_selector(
	$for,
	$states : null,
	$values : null
) {
	$sel : ();

	@if length($for) > 1 {
		@each $f in $for {
			$sel : append($sel, gridle_selector($f, $states, $values), comma);
		}
	} @else {

		// get all states if not specified
		@if $states == null {
			$states : gridle_get_states_names();
		}

		// get the pattern
		$pattern : map-get($_gridle-packages, $for);
		$pattern : map-get($pattern, classname);

		@each $stateName in $states {
			@if $values != null {
				$sel : append($sel, _gridle_classname($for, $stateName, $values), comma);
			} @else if index($pattern, '%column') {
				@each $columnName, $column in _gridle_get_pattern_values(column) {
					$sel : append($sel, _gridle_classname($for, $stateName, $columnName), comma);
				}
			} @else if index($pattern, '%column-count') {
				@for $i from 0 through _gridle_get_pattern_values(column-count) {
					$sel : append($sel, _gridle_classname($for, $stateName, $i), comma);
				}
				@if $for == flex-order {
					$sel : append($sel, _gridle_classname($for, $stateName, first), comma);
					$sel : append($sel, _gridle_classname($for, $stateName, last), comma);
				}
			} @else if index($pattern, '%align') {
				@each $a in _gridle_get_pattern_values(align) {
					$sel : append($sel, _gridle_classname($for, $stateName, $a), comma);
				}
			} @else if index($pattern, '%count') and $for == clear-each {
				@each $idx, $clearEach in _gridle_get_pattern_values(count) {
					$count : map-get($clearEach, clearEach);
					$sel : append($sel, _gridle_classname($for, $stateName, $count), comma);
				}
			} @else if index($pattern, '%side') {
				@each $side in _gridle_get_pattern_values(side) {
					$sel : append($sel, _gridle_classname($for, $stateName, $side), comma);
				}
			} @else if index($pattern, '%float') {
				@each $float in _gridle_get_pattern_values(float) {
					$sel : append($sel, _gridle_classname($for, $stateName, $float), comma);
				}
			} @else if index($pattern, '%reverse') {
				@each $reverse in _gridle_get_pattern_values(reverse) {
					$sel : append($sel, _gridle_classname($for, $stateName, $reverse), comma);
				}
			} @else {
				$sel : append($sel, _gridle_classname($for, $stateName), comma);
			}
		}
	}
	@return $sel;
}


//
// Generate classname
//
// @param 	List 		$pattern 	The pattern to use to generate classname
// @param 	String 		$state 		The state
// @param 	int 		$count 		The column count
//
@function _gridle_classname(
	$for,
	$state : null,
	$value : null
) {

	// get the pattern
	$pattern : $for;
	@if type-of($for) == string {
		$pattern : map-get($_gridle-packages, $for);
		$pattern : map-get($pattern, classname);
	}

	// delete default :
	@if unquote("#{$state}") == default {
		$state : null;
	}

	// construct class name :
	$removeSeparator : false;
	@for $i from length($pattern) through 1 {
		$var : nth($pattern, $i);

		@if $var == '@' {
			$pattern : set-nth($pattern, $i, '\\@');
		}

		@if index($_gridle_names-separators, $var) {
			// check if need to remove separator
			@if $removeSeparator {
				$pattern : set-nth($pattern, $i, null);
			}
			$removeSeparator : false;
		} @else if $var == "%state" {
			@if $state == null {
				$pattern : set-nth($pattern, $i, null);
				$removeSeparator : true;
			} @else {
				$pattern : set-nth($pattern, $i, $state);
			}
		} @else if $var and str-index($var, '%') == 1 {
			$token : str-slice($var, 2);
			// check that the value is part of the token
			$tokens : map-get($_gridle-names-tokens, $token);
			@if $tokens {
				$pattern : set-nth($pattern, $i, $value);
				@if $value == null {
					$removeSeparator : true;
				}
			}
		} @else if $var == null {
			$removeSeparator : true;
		}

	}

	// clean selector
	$list: ();
	@each $var in $pattern {
		@if $var {
			$list: append($list, $var);
		}
	}
	$pattern : $list;

	// build selector
	$sel : "";
	$prefix : gridle_get_state_var(classes-prefix, $state);
	@each $part in $pattern {
		@if $part {
			$sel : "#{$sel}#{$part}";
		}
	}
	@if $prefix and str-slice($sel,1,str_length($prefix)) != $prefix {
		$sel : ".#{$prefix}#{$sel}";
	} @else {
		$sel : ".#{$sel}";
	}

	// return generated class :
	@return unquote($sel);
}


//
// Unmatched patterns
//
// @param 	List 		$pattern 	The pattern to use to generate classname
// @return  Map                     Map of unmatched pattern index:pattern
//
@function _gridle_unmatched_patterns(
	$pattern
) {
	$unmatched : ();

	// Loop each partern
	@for $i from length($pattern) through 1 {
		$var : nth($pattern, $i);
		@if $var and str-index($var, '%') == 1 {
			$token : str-slice($var, 2);
			// check that the value is part of the token
			$tokens : map-get($_gridle-names-tokens, $token);
			@if $tokens == null {
				$unmatched : map-set($unmatched, $i, $var);
			}
		}
	}

	@return $unmatched;
}


//
// matched patterns
//
// @param 	List 		$pattern 	The pattern to use to generate classname
// @return  Map                     Map of matched pattern index:pattern
//
@function _gridle_matched_patterns(
	$pattern
) {
	$matched : ();

	// Loop each partern
	@for $i from length($pattern) through 1 {
		$var : nth($pattern, $i);
		@if $var and str-index($var, '%') == 1 {
			$token : str-slice($var, 2);
			// check that the value is part of the token
			$tokens : map-get($_gridle-names-tokens, $token);
			@if $tokens != null {
				$matched : map-set($matched, $i, $var);
			}
		}
	}

	@return $matched;
}


//
// Get dynamic pattern values
//
// @param 	String 		$for 			Name of the token
// @return 	Mixed 					    Values of the token
//
@function _gridle_get_pattern_values(
	$for
){
	@if $for == column {
		@return gridle_get_columns();
	}

	@if $for == column-count {
		@return length(gridle_get_columns());
	}

	@if $for == count {
		@return $_gridle_clear_classes;
	}

	@return map-get($_gridle-names-tokens, $for);
}


//
// Get the current driver
//
@function gridle_get_driver() {
	@return $_gridle-driver;
}


//
// Is driver
//
@function gridle_is_driver($driver) {
	@each $d in $driver {
		@if $d == gridle_get_driver() {
			@return true;
		}
	}
	@return false;
}


//
// Check if we need to generate the class or not
//
// @param 	List 		$for 			Name of the class map
// @param 	List 		$what 			The map that set which class map to include and exclude
// @return 	Boolean 					true if need to generate, false if not
//
@function _gridle_need_to_generate(
	$package,
	$what
) {
	// check that the wanted package exist in system
	@if map-get($_gridle-packages, $package) {
		$package : map-get($_gridle-packages, $package);
		$package : map-get($package, package);
	} @else {
		@return false;
	}

	// if we have a what param, need to check if the package is needed
	@if $what and $what != all {

		// check if we have only some - in the states list
		// mean that we want to only remove these specified states
		// from the all states list
		$onlyRemove : true;
		@each $w in $what {
			@if str-slice($w,1,1) != '-' {
				$onlyRemove : false;
			}
		}

		@if $onlyRemove {
			@each $name in $package {
				@if index($what, unquote("-#{$name}")) {
					@return false;
				}
			}
			@return true;
		} @else {
			@each $name in $package {
				@if index($what, unquote("#{$name}")) {
					@return true;
				}
			}
			@return false;
		}
	}
	// we don't have a what param so the package is
	@return true;
}


//
// Get generic selector for a class
//
@function _gridle_get_generic_selector(
	$package
) {
	$p : map-get($_gridle-packages, $package);
	$sel : map-get($p, generic-selector);
	@if $sel {
		@return unquote($sel);
	}
	$sel : map-get($p, classname);
	$generic : "";
	$end : false;
	$i : 1;
	@each $part in $sel {
		@if not $end {
			// if we have a separator, check the next value to see if it's a variable
			@if index($_gridle-names-separators, $part) and $i < length($sel) {
				@if nth($sel,$i + 1) != '%state' {
					$generic : #{$generic}#{$part};
				} @else {
					$end : true;
				}
			} @else {
				@if str-slice($part,1,1) != '%' {
					$generic : #{$generic}#{$part};
				} @else {
					$end : true;
				}
			}
		}
		$i : $i + 1;
	}
	@return unquote('[class*="#{$generic}"]');
}


//
// Check if gridle is in generation phase
//
@function _gridle_is_in_generate_phase() {
	@return $_gridle_is_in_generate_phase;
}


//
// Parse gridle mixin list
//
@function _gridle_parse_gridle_mixin_list(
	$list
) {
	// map
	$map : ();

	// context
	$context : null;
	$of : index($list, of);
	@if $of {
		$context : nth($list, ($of + 1));
		$list : remove-nth($list,$of);
		$list : remove-nth($list,$of);
	} @else {
		$ctx : index($list, context);
		@if $ctx {
			$context : nth($list, ($ctx + 1));
			$list : remove-nth($list,$ctx);
			$list : remove-nth($list,$ctx);
		}
	}

	// grid
	$gr : nth($list, 1);
	@if type-of($gr) == number or $gr == adapt or $gr == grow or gridle_has_column($gr) {
		$map : map-set($map, grid, $gr $context);
		$list : remove-nth($list,1);
	}

	// prefix
	$prefix : null;
	$at : index($list, at);
	@if $at {
		$map : map-set($map, prefix, nth($list,($at + 1)) $context);
		$list : remove-nth($list,$at);
		$list : remove-nth($list,$at);
	}

	// loop on each parameters
	$prop_values : ();
	$prop_name : null;
	$list : append($list, _gridle-last);
	@each $param in $list {

		@if $param != with and $param != 'and' {
			@if map-get($_gridle-packages, $param) or $param == _gridle-last {
				@if $prop_name {
					$values : true;
					@if length($prop_values) > 0 {
						$values : $prop_values;
						@if length($prop_values) == 1 {
							$values : nth($prop_values,1);
						}
						$prop_values : ();
					}
					$map : map-set($map, $prop_name, $values);
				}
				$prop_name : $param;
			} @else {
				$prop_values : append($prop_values, $param);
			}
		}
	}
	
	// // check if we don't have any gutter parameters
	// @if not map-get($map, gutter) and not map-get($map, no-gutter) {
	// 	$map : map-set($map, gutter, true);
	// }

	// return the map
	@return $map;
}


//
// Get set map from string
//
@function _gridle_get_gridle_set_map_from_list(
	$list
) {
	// loop on each list element to split into states lists
	$state : current;
	$map : ();
	@each $prop in $list {
		@if map-get($map, $state) == null {
			$map : map-set($map, $state, ());
		}
		@if type-of($prop) == string and gridle_has_state($prop) {
			$state : $prop;
		} @else {
			$ls : map-get($map, $state);
			$ls : append($ls, $prop);
			$map : map-set($map, $state, $ls);
		}
	}

	// loop on each states
	@each $stateName, $props in $map {
		@if length($props) > 0 {
			$props_map : _gridle_parse_gridle_mixin_list($props);
			@if type-of($props_map) == map {
				$map : map-set($map, $stateName, $props_map);
			} @else {
				$map : map-set($map, $stateName, null);
			}
		}
	}
	
	@return $map;
}


//
// Get the media query for a particular state, or width, etc...
//
// @param 	Mixed 		$state-or-min-width 		The state name of the min width
// @param 	Mixed 		$max-width 			The max width if first param is a min width
// @return 	String 						The media query string without the @media
//
@function gridle_get_media_query(
	$state-or-settings : current
) {
	// check if is a string :
	$state : null;
	@if type-of($state-or-settings) == string
	{
		$state : gridle_get_state($state-or-settings);
	}
	@else if $state-or-settings == null
	{
		$state : $_gridle-settings;
	}
	@else
	{
		$state : map-merge($_gridle-settings, $state-or-settings);
	}

	// if it's some settings or a state :
	@if $state {

		// get vars :
		$name : map-get($state, name);
		$min-width : map-get($state, min-width);
		$max-width : map-get($state, max-width);
		$query : map-get($state, query);

		// direct query :
		@if $query
		{
			@return $query;
		}
		@else if $min-width and $max-width
		{
			@return "screen and (min-width: #{$min-width}) and (max-width: #{$max-width})";
		}
		@else if $min-width
		{
			@return "screen and (min-width: #{$min-width})";
		}
		@else if $max-width
		{
			@return "screen and (max-width: #{$max-width})";
		}
		@else
		{
			@return null;
		}

	}
	@else
	{
		@return null;
	}
}
